Mac OS Sound

| Previous | Chapter Contents | Chapter Top | Next |

An Example of Converting a Buffer of Silence to IMA 4:1

The following is an example of how to use the sound conversion architecture to convert a buffer of silence to IMA 4:1, changing the sampling rate in the process.

enum {
    kTargetBytes = 20 * 1024
};

void main(void)
{
    SoundConverter sc;
    SoundComponentData inputFormat, outputFormat;
    unsigned long inputFrames, inputBytes;
    unsigned long outputFrames, outputBytes;
    Ptr inputPtr, outputPtr;
    OSErr err;

    inputFormat.flags = 0;
    inputFormat.format = kOffsetBinary;
    inputFormat.numChannels = 1;
    inputFormat.sampleSize = 8;
    inputFormat.sampleRate = rate22050hz;
    inputFormat.sampleCount = 0;
    inputFormat.buffer = nil;
    inputFormat.reserved = 0;

    outputFormat.flags = 0;
    outputFormat.format = kIMA4SubType;
    outputFormat.numChannels = 1;
    outputFormat.sampleSize = 16;
    outputFormat.sampleRate = rate44100hz;
    outputFormat.sampleCount = 0;
    outputFormat.buffer = nil;
    outputFormat.reserved = 0;

    err = SoundConverterOpen(&inputFormat, &outputFormat, &sc);
    if (err != noErr)
        DebugStr("\pOpen failed");

    err = SoundConverterGetBufferSizes(sc, kTargetBytes,
                    &inputFrames, &inputBytes, &outputBytes);
    if (err != noErr)
        DebugStr("\pGetBufferSizes failed");

inputPtr = NewPtrClear(inputBytes);
outputPtr = NewPtrClear(outputBytes);

// fill input buffer with 8-bit silence
{
    int i;
    Ptr dp = inputPtr;

    for (i = 0; i < inputBytes; i++)
        *dp++ = 0x80;
}

    err = SoundConverterBeginConversion(sc);
    if (err != noErr)
        DebugStr("\pBegin Conversion failed");

    err = SoundConverterConvertBuffer(sc, inputPtr, inputFrames,
                    outputPtr, &outputFrames, &outputBytes);
    if (err != noErr)
        DebugStr("\pConversion failed");

    err = SoundConverterEndConversion(sc,
                outputPtr,&outputFrames, &outputBytes);
    if (err != noErr)
        DebugStr("\pEnd Conversion failed");

    err = SoundConverterClose(sc);
    if (err != noErr)
        DebugStr("\pClose failed");
}

© 1998 Apple Computer, Inc.

| Previous | Chapter Contents | Chapter Top | Next |